package com.breaking.second.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.fastjson2.JSON;
import com.breaking.second.activity.BaseActivity;
import com.breaking.second.activity.PagerActivity;
import com.breaking.second.application.EscortApplication;
import com.breaking.second.databinding.ActivityLoginBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.UserLoginEntity;
import com.breaking.second.utils.SecurePreferencesUtil;
import com.breaking.second.utils.StringUtil;
import com.breaking.second.utils.okhttp.OkhttpUtil;

import java.io.IOException;

import okhttp3.FormBody;

//登录
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        binding.login.setOnClickListener(this::login);
        binding.register.setOnClickListener(this::register);
    }

    private void register(View view) {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    private void login(View view) {
        Editable mail = binding.user.getText();
        Editable pass = binding.pass.getText();
        if (StringUtil.isEmpty(mail)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        } else if (StringUtil.isEmpty(pass)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            login(mail.toString(), pass.toString());
        }
    }

    private void login(String user, String pass) {
        FormBody.Builder form = new FormBody.Builder();
        form.add("username", user);
        form.add("password", pass);
        OkhttpUtil.Post("user/login", form, new OkhttpUtil.CallbackListener(this::login));
    }

    private void login(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            UserLoginEntity userLoginEntity = bookEntity.to(UserLoginEntity.class);
            if (userLoginEntity == null) return;
            SecurePreferencesUtil securePreferencesUtil = new SecurePreferencesUtil(UserLoginEntity.class.getName());
            securePreferencesUtil.saveEncryptedString(this, "data", JSON.toJSONString(userLoginEntity));
            startActivity(new Intent(this, PagerActivity.class));
            EscortApplication.setUserLoginEntity(userLoginEntity);
            finish();
        }
        Toast.makeText(this, bookEntity.getMsg(), Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 123) {
            if (data == null) return;
            String mail = data.getStringExtra("user");
            String pass = data.getStringExtra("pass");
            binding.user.setText(mail);
            binding.pass.setText(pass);
        }
    }
}
